<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * The project “dev”
 * File name “Menu_model.php”
 *
 * User: znx
 * Company: 思议创想
 * Q  Q: 865303386
 * Site: www.siyi360.com
 *
 * Date: 2017-02-09
 * Time: 15:25
 */
class Main_model extends CI_Model {
    /**
     * get_menu
     * 获取管理后台系统菜单数据
     *
     * @param int $status 菜单状态 默认显示全部 1=显示 0=不显示
     * @return mixed
     */
    public function get_menu($status=null) {
        if (isset($status)) $this->db->where('menu_status', $status);
        $this->db->order_by('menu_sort DESC');
        $query = $this->db->get('admin_menu');

        return $query->result_array();
    }

    /**
     * user_safe
     * 当前登录的系统用户更新账号信息
     *
     * @param int $user_id 系统用户id
     */
    public function user_safe($user_id)
    {
        $encryptPassword = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);

        $form = $this->input->post();
        $data = array();

        if (!empty($form['user_pass'])) $data['user_pass'] = $encryptPassword->HashPassword($form['user_pass']);
        $data['user_email'] = $form['user_email'];

        // 更新数据
        $result = $this->db->where('user_id',$user_id)->update('admin_user', $data);

        if ($result) {
            if (array_key_exists('user_pass', $data)) {
                $this->session->sess_destroy(); // 销毁session

                return array('status'=>1, 'msg'=>'操作成功，请重新登录', 'result'=>array('jumpUrl'=>site_url('login')));
            } else {
                return array('status'=>1, 'msg'=>'操作成功', 'result'=>'');
            }
        } else {
            return array('status'=>0, 'msg'=>'操作失败', 'result'=>'');
        }
    }
}